<template>
	<div class="term-history">
		<nav-header></nav-header>
		<div class="zh-table">
			<div class="zh-table-open clear">
                <div class="zh-open-left fl">
                    <div class="open-width" style="width: 100px">
                        <el-select v-model="listParams.start_year" filterable placeholder="开始学年">
                            <el-option
                                v-for="item in syearData"
                                :key="item.id"
                                :label="item.value"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="open-width" style="margin-right: 10px;width: 10px">-</div>
                    <div class="open-width" style="width: 100px">
                        <el-select v-model="listParams.end_year" filterable placeholder="结束学年">
                            <el-option
                                v-for="item in eyearData"
                                :key="item.id"
                                :label="item.value"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="open-width" style="width: 110px">
                        <el-select v-model="listParams.semester" filterable placeholder="请选择学期">
                            <el-option label="全部学期" value="0"></el-option>
                            <el-option label="第一学期" value="1"></el-option>
                            <el-option label="第二学期" value="2"></el-option>
                        </el-select>
                    </div>
                    <el-button type="primary" @click="searchFunc">搜索</el-button>
                    <el-button type="primary" @click="reset">重置</el-button>
                </div>
			</div>
            <div class="zh-table-contents"
				v-loading="loading.status"
				:element-loading-text="loading.txt">
                <el-table
				    :data="listData"
				    border
				    max-height="600"
				    style="width: 100%">
				    <el-table-column
				      prop="year"
				      label="学年"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
                    <el-table-column
				      prop=""
				      label="学期"
				      align="center"
				      show-overflow-tooltip
				      >
	      		      	<template slot-scope="scope">
                                <span v-if="scope.row.semester === 1">{{'第一学期（秋季）'}}</span>
                                <span v-if="scope.row.semester === 2">{{'第二学期（春季）'}}</span>
	      	      	    </template>
				    </el-table-column>
                    <el-table-column
				      prop="start_time"
				      label="开始日期"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
                    <el-table-column
				      prop="end_time"
				      label="结束日期"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
                    <el-table-column
				      prop="weeks"
				      label="教学周"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
				</el-table>
                <div class="table-pagination" v-show="total_number > 10">
					<el-pagination
					    @size-change="handleSizeChange"
					    @current-change="handleCurrentChange"
					    :current-page="listParams.page_index"
					    :page-sizes="[10, 20, 30, 40, 50, 100]"
					    :page-size="listParams.page_size"
					    layout="total, sizes, prev, pager, next, jumper"
					    :total="total_number">
					</el-pagination>
					<div class="pagination-button">
						<el-button 
							@click="getList"
							type="primary" icon="el-icon-refresh" 
							circle title="刷新数据" size="mini" >
						</el-button>
					</div>
				</div>
            </div>
		</div>
	</div>
</template>
<script>
    export default {
        data() {
            return {
                loading: {
                    status:false,
					txt:'获取数据...'
                },
                listData: [],
                total_number: 0,
                listParams: {
                    start_year: null,
                    end_year: null,
                    semester: '0',
                    page_index: 1,
                    page_size: 10
                },
                syearData: [],
                eyearData: [],
            }
        },
        created() {
            this.getList();
            this.getYear();
        },
        watch: {
            'listParams.start_year': function(val, oldval) {
				if (this.listParams.end_year !== null) {
                    if (parseInt(this.getYearValue(val, this.syearData)) >= parseInt(this.getYearValue(this.listParams.end_year, this.eyearData))) {
                        this.$message.error('开始时间要小于结束时间');
                        this.listParams.start_year = oldval;
                    }
                }
            },
            'listParams.end_year': function(val, oldval) {
				if (this.listParams.start_year !== null) {
                    if (parseInt(this.getYearValue(val, this.eyearData)) <= parseInt(this.getYearValue(this.listParams.start_year, this.syearData))) {
                        this.$message.error('结束时间要大于开始时间');
                        this.listParams.end_year = oldval;
                    }
                }
			}
        },
        methods: {
            getYearValue(val, obj) {
                let temp = obj.filter(item => {
                    if (item.id == val) {
                        return true;
                    }
                })

                return temp[0].value;
            },
            getYear() {
				let success = (res => {
                    let temp = deep_clone(res.data);
                    let start_year = temp.start_year ? temp.start_year.start_year : null;
                    let end_year = temp.end_year ? temp.end_year.end_year : null;
                    this.syearData = [];
                    this.eyearData = [];

                    if (end_year > start_year + 1) {
                        let len = end_year-start_year+1;

                        for (let i=1; i<=len; i++) {
                            this.syearData.push({
                                id: i,
                                value: start_year++
                            })
                        }

                        this.eyearData = deep_clone(this.syearData);
                        this.eyearData.shift();
                        this.eyearData.unshift({id: 0, value: '--'});
                        this.syearData.unshift({id: 0, value: '--'});
                        this.syearData.pop();
                    } else {
                        this.syearData.push({id: 0, value: '--'}, {id: 1, value: start_year})
                        this.eyearData.push({id: 0, value: '--'}, {id: 1, value: end_year})
                    }
				});
				let fail = ( data => {
					this.$message.error(data.msg);
                })
                
  				this.$ajax.get('api/term/get_history_year', {params: {}, func: {success: success, fail: fail}})
            },
            /**
            * 搜索列表
            */
            searchFunc() {
                this.listParams.page_index = 1;
                this.getList();
            },
            reset() {
                this.listParams.start_year = null;
                this.listParams.end_year = null;
                this.listParams.semester = '0';
                this.listParams.page_index = 1;
                this.listParams.page_size = 10;
                this.getList();
            },
            handleSizeChange(val) {
                this.listParams.page_index = 1;
		    	this.listParams.page_size = val;
		    	this.getList()
            },
            handleCurrentChange(val) {
                this.listParams.page_index = val;
		    	this.getList();
            },
            getList() {
                this.loading.status = true;

				let success = ( data => {
					this.loading.status = false;
                    this.listData = deep_clone(data.data.data);
                    this.total_number = data.data.data_sum;

                    if (this.listData.length > 0) {
                        this.listData.forEach(item => {
                            if (item.start_year === item.end_year) {
                                item.year = (parseInt(item.start_year) - 1) + '-' + item.end_year + ' 学年';
                            } else {
                                item.year = item.start_year + '-' + item.end_year + ' 学年';
                            }
                            item.start_time = item.start_time.substr(0,10);
                            item.end_time = item.end_time.substr(0,10);
                        })
                    }
				});
				let fail = ( data => {
					this.loading.status = false;
					this.$message.error(data.msg);
                })

                let params = {};

                this.syearData.forEach(item => {
                    if (item.id === this.listParams.start_year) {
                        params.start_year = item.value
                    }
                })

                this.eyearData.forEach(item => {
                    if (item.id === this.listParams.end_year) {
                        params.end_year = item.value
                    }
                })

                params.page_index = this.listParams.page_index;
                params.page_size = this.listParams.page_size;
                params.semester = this.listParams.semester;
                
  				this.$ajax.get('api/term/get_last_term', {params: params, func: {success: success, fail: fail}})
            },
        },
    }
</script>
<style lang="scss">
    .term-history {

    }
</style>
